package com.yunxin.customui.service.createimpl;


import cn.hutool.core.util.ObjectUtil;
import com.yunxin.customui.util.PageUtil;
import com.yunxin.customui.bean.card.CardBean;
import com.yunxin.customui.bean.card.CardConfig;
import com.yunxin.customui.service.CustomPageTemplate;
import com.yunxin.customui.myinterface.ICustomUIHandler;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 自定义卡
 *
 * @author LGY
 * @date 2021/09/26
 */
@Component
public class CustomCardImpl extends CustomPageTemplate<CustomCardImpl.CardData, CardConfig, CardBean, Map<String,Object>> {

    @Autowired
    ApplicationContext applicationContext;

    @Override
    protected CardData getData(CardConfig config,  Map<String,Object> paramMap) {
        //根据配置和参数 执行策略，得到
        CardData cardData = new CardData();
        String imgInit = config.getImgInit();
        if (ObjectUtil.isNotNull(imgInit)){
            //策略
            List<PageUtil.MethodParam> methodParams = PageUtil.analysisMethod(imgInit);
            for (PageUtil.MethodParam methodParam : methodParams) {
                ICustomUIHandler cardHandler = applicationContext.getBean(methodParam.getMethodName(), ICustomUIHandler.class);
                Object handler = cardHandler.handle(methodParam.getParams(), paramMap);
                cardData.setBytes((Byte[])handler);
            }
        }else{
            //设置默认值
            cardData.setImg(config.getImgURL());
        }
        String labelInit = config.getLabelInit();
        if (ObjectUtil.isNotNull(labelInit)){
            //策略
            List<PageUtil.MethodParam> methodParams = PageUtil.analysisMethod(labelInit);
            for (PageUtil.MethodParam methodParam : methodParams) {
                ICustomUIHandler cardHandler = applicationContext.getBean(methodParam.getMethodName(), ICustomUIHandler.class);
                Object handler = cardHandler.handle(methodParam.getParams(), paramMap);
                cardData.setLabel(handler.toString());
            }
        }else{
            //设置默认值
            cardData.setLabel(config.getLabelDesc());
        }
        String textInit = config.getTextInit();
        if (ObjectUtil.isNotNull(textInit)){
            //策略
            List<PageUtil.MethodParam> methodParams = PageUtil.analysisMethod(textInit);
            for (PageUtil.MethodParam methodParam : methodParams) {
                ICustomUIHandler cardHandler = applicationContext.getBean(methodParam.getMethodName(), ICustomUIHandler.class);
                Object handler = cardHandler.handle(methodParam.getParams(), paramMap);
                cardData.setText(handler.toString());
            }
        }else{
            //设置默认值
            cardData.setText(config.getText());
        }
        return cardData;
    }

    @Override
    protected CardBean build(CardData cardData, CardConfig config) {
        CardBean cardBean = new CardBean(config);
        if(cardData.getImg() != null){
            cardBean.setImgURL(cardData.getImg());
        }
        if(cardData.getLabel()!= null){
            cardBean.setLabelDesc(cardData.getLabel());
        }
        if(cardData.getText() != null){
            cardBean.setText(cardData.getText());
        }
        if(cardData.getBytes() != null){
            cardBean.setImgBinary(cardData.getBytes());
        }
        return cardBean;
    }

    @Data
    static class CardData{
        String img;
        String label;
        String text;
        Byte[] bytes;
    }


}
